﻿using MediatR;
using S.EarTag.Domain.ApiResult;
using S.EarTag.Domain.Model;
using S.EarTag.Infrastructure.Repository.Interface.IEarTagRepository;
using S.EarTag.Read.API.Mediator.MediatorCommand.EarTagCommand;

namespace S.EarTag.Read.API.Mediator.MediatorCommandHandler
{
    public class EarTagFindListCommandHandler : IRequestHandler<EarTagFindListCommand, ApiResult<List<EarTagModel>>>
    {
        private readonly IEarTagModelRepository _earTagModelRepository;
        public EarTagFindListCommandHandler(IEarTagModelRepository earTagModelRepository)
        {
            _earTagModelRepository = earTagModelRepository;
        }
        /// <summary>
        /// 二标条件查询+分页查询显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<List<EarTagModel>>> Handle(EarTagFindListCommand request, CancellationToken cancellationToken)
        {
            
            var list = await _earTagModelRepository.GetEarTagModelsList(request.EarTag,request.BreedItem,request.BirthState,request.BindState);

            //var list = await  _earTagModelRepository.GetAllAsync();

            ApiResult<List<EarTagModel>> apiResult=new ApiResult<List<EarTagModel>>();
            apiResult.Data= list;
            //总页数、总条数
            apiResult.totalCount = list.Count();
            apiResult.pageCount = (int)Math.Ceiling((apiResult.totalCount * 1.0) / request.pageSize);
            //分页
            apiResult.Data = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return apiResult;
        }
    }
}
